package bean.managed;

import bean.session.ContactEntityFacade;
import bean.session.RawMaterialEntityFacade;
import bean.session.VendorProductPriceEntityFacade;
import entity.VendorEntity;
import bean.managed.util.JsfUtil;
import bean.managed.util.PaginationHelper;
import bean.session.VendorEntityFacade;

import entity.AddressEntity;
import entity.ContactEntity;
import entity.RawMaterialEntity;
import entity.VendorProductPriceEntity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.application.ViewHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
import managedbean.AbstractBacking;
import org.primefaces.event.RowEditEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.ToggleEvent;
import org.primefaces.model.DualListModel;
import type.AddressType;
import type.Country;
import type.VendorType;

@ManagedBean(name = "vendorEntityController")
@SessionScoped
public class VendorEntityController extends AbstractBacking implements Serializable {

    @ManagedProperty(value = "#{contactEntityController}")
    private ContactEntityController contactEntityController;

    public DualListModel<String> getRmsPickList() {

        List<RawMaterialEntity> allRMEntityList = new ArrayList<RawMaterialEntity>();

        allRMs.clear();
        allRMEntityList = facade.findAllIncludeArchived();

        for (int i = 0; i < allRMEntityList.size(); i++) {

            allRMs.add(allRMEntityList.get(i).getProductName());

        }

        rmsPickList = new DualListModel<String>(allRMs, choosenRMs);

        return rmsPickList;

    }
//for picklist
    List<String> allRMs = new ArrayList<String>();
    List<String> choosenRMs = new ArrayList<String>();
    private DualListModel<String> rmsPickList;

    public List<String> getAllRMs() {
        return allRMs;
    }

    public void setAllRMs(List<String> allRMs) {
        this.allRMs = allRMs;
    }
    public Country[] getCountry() {
        return Country.values();
    }
    public List<String> getChoosenRMs() {
        return rmsPickList.getTarget();
    }

    public void setChoosenRMs(List<String> choosenRMs) {
        this.choosenRMs = choosenRMs;
    }

    public void setRmsPickList(DualListModel<String> rmsPickList) {
        this.rmsPickList = rmsPickList;
    }

    public boolean checkVendorProduct(VendorProductPriceEntity vpp) {
        List<String> choosenRMs = rmsPickList.getTarget();
        for (String rm : choosenRMs) {
            if (vpp.getRawMaterial().getProductName().equalsIgnoreCase(rm)) {
                return true;
            }

        }
        return false;
    }
    
public void handleToggle(ToggleEvent event) {  
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Edit Raw Material ", ""+ event.getVisibility());  
  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }  
    public String update() {
        List<String> choosenRMs = rmsPickList.getTarget();
        Collection<RawMaterialEntity> rmss = new ArrayList<RawMaterialEntity>();
        for (String rm : choosenRMs) {
            RawMaterialEntity r = getFacade().findRawMaterial(rm);
            rmss.add(r);
        }
        currVendor.setRms(rmss);
        getEjbFacade().edit(currVendor);
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("Supplier info updated", "Supplier info updated"));

        return "EditVendorRawMaterial?faces-redirect=true";
    }
    private Collection<VendorEntity> vendors;

    public Collection<VendorEntity> getVendors() {
        return getEjbFacade().getCurrentVendor();
    }

    public void setVendors(Collection<VendorEntity> vendors) {
        this.vendors = vendors;
    }
    private List<VendorEntity> unconfirmedVendors;
    private VendorEntity vendor;
    private List<RawMaterialEntity> rawMaterials;
    @EJB
    private bean.session.RawMaterialEntityFacade facade;

    public RawMaterialEntityFacade getFacade() {
        return facade;
    }
    @EJB
    private bean.session.VendorEntityFacade ejbFacade;

    public RawMaterialEntityFacade facade() {
        return facade;
    }

    public void setFacade(RawMaterialEntityFacade facade) {
        this.facade = facade;
    }

    public List<String> getRawMaterials() {
        rawMaterials = facade.findAllIncludeArchived();
        List<String> rms = new ArrayList<String>();
        for (RawMaterialEntity rm : rawMaterials) {
            rms.add(rm.getProductName());
        }
        return rms;
    }

    public void setRawMaterials(List<RawMaterialEntity> rawMaterials) {
        this.rawMaterials = rawMaterials;
    }

    public VendorEntity getVendor() {
        if (getLoginVendor() != null) {
            return getLoginVendor();
        }
        return null;
    }
  private boolean doneUpdate;

    public boolean isDoneUpdate() {
        return doneUpdate;
    }

    public void setDoneUpdate(boolean doneUpdate) {
        this.doneUpdate = doneUpdate;
    }
    public void setVendor(VendorEntity vendor) {
        this.vendor = vendor;
    }
    private VendorEntity selectedVendor;

    public VendorEntity getSelectedVendor() {
        return selectedVendor;
    }

    public void setSelectedVendor(VendorEntity selectedVendor) {
        this.selectedVendor = selectedVendor;
    }

    public VendorEntityFacade getEjbFacade() {
        return ejbFacade;
    }

    public void setEjbFacade(VendorEntityFacade ejbFacade) {
        this.ejbFacade = ejbFacade;
    }

    public VendorEntityController() {
    }

    public List<VendorEntity> getUnconfirmedVendors() {
        unconfirmedVendors = getEjbFacade().getUnconfirmedVendor();

        return unconfirmedVendors;
    }
//
//    public void update() {
//        System.out.println("kktttttttt");
//        Collection<VendorEntity> cust = getVpps();
//        for (VendorEntity c : cust) {
//
//            System.out.println(c.getUnitPrice());
//            getVfacade().edit(c);
//
//        }
//        getVendor().setVendorProductPriceList(getVpps());
//        getEjbFacade().edit(getVendor());
//
//
//    }

    public String deleteSupplier() {

        getSelectedVendor().setLocked(true);
        getEjbFacade().edit(getSelectedVendor());
        //to refresh page
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("Supplier Deleted", "Supplier Deleted"));
        return "ListSupplier?faces-redirect=true";

    }

    public String createRedirectToIndex() {
        try {

            createWithoutRedirect();
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage("Supplier Created", "Supplier Created"));
            current = null;
            return "";
        } catch (Exception e) {
            e.printStackTrace();
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public void createWithoutRedirect() throws Exception {
        List<String> choosenRMs = rmsPickList.getTarget();
        Collection<RawMaterialEntity> rmss = new ArrayList<RawMaterialEntity>();
        for (String rm : choosenRMs) {
            RawMaterialEntity r = getFacade().findRawMaterial(rm);
            rmss.add(r);
        }
        current.setRms(rmss);
        current.setVendorType(VendorType.confirmed);
        address1.setType(AddressType.HomeAddress);
        addresse1.setType(AddressType.BillingAddress);
        Collection<AddressEntity> addr = new ArrayList<AddressEntity>();
        addr.add(address1);
        addr.add(addresse1);
        current.getContact().setAddresses(addr);
        current.getContact().setPerson(current);
        current.setStar(3.0);
        current.created(getLoginPerson());
        getEjbFacade().create(current);
        address1 = new AddressEntity();
        addresse1 = new AddressEntity();

    }
    //add new supplier
    private VendorEntity current;

    public VendorEntity getSelected() {
        if (current == null) {
            current = new VendorEntity();

        }

        return current;
    }
    private VendorEntity currVendor;

    public VendorEntity getCurrVendor() {
        return currVendor;
    }

    public void setCurrVendor(VendorEntity currVendor) {
        this.currVendor = currVendor;
    }

    public void setSelected(VendorEntity current) {
        this.current = current;
    }
    private VendorEntity selectedV;

    public VendorEntity getSelectedV() {
        return selectedV;
    }

    public void setSelectedV(VendorEntity selectedV) {
        this.selectedV = selectedV;
    }

    public String vendorSelect(SelectEvent event) {
        currVendor = selectedV;
        return "RawMaterialList?faces-redirect=true";
    }
    private Collection<RawMaterialEntity> rms;

    public Collection<RawMaterialEntity> getRms() {
        return selectedVendor.getRms();
    }

    public void setRms(Collection<RawMaterialEntity> rms) {
        this.rms = rms;
    }

    public String prepareEdit() {
        current = selectedVendor;
        resetContactAndAddresses();

        return "Edit?faces-redirect=true";
    }

    public ContactEntityController getContactEntityController() {
        return contactEntityController;
    }

    public void setContactEntityController(ContactEntityController contactEntityController) {
        this.contactEntityController = contactEntityController;
    }

    private void resetContactAndAddresses() {
        contactEntityController.setupCurrent(current.getContact());
    }

    public String update1() {
        List<String> choosenRMs = rmsPickList.getTarget();
        Collection<RawMaterialEntity> rmss = new ArrayList<RawMaterialEntity>();
        for (String rm : choosenRMs) {
            RawMaterialEntity r = getFacade().findRawMaterial(rm);
            rmss.add(r);
        }
        if(choosenRMs.size()!=0)
        current.setRms(rmss);


        ContactEntity c = current.getContact();
        Collection<AddressEntity> addr = new ArrayList<AddressEntity>();
        addr.add(address);
        addr.add(addresse);

        c.setAddresses(addr);
        getcFacade().edit(c);
        current.modified(getLoginPerson());
        getEjbFacade().edit(current);
        current = new VendorEntity();;
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("Updated", "Supplier Info Updated"));
        return "Vw";

    }
    @EJB
    private bean.session.ContactEntityFacade cFacade;

    public ContactEntityFacade getcFacade() {
        return cFacade;
    }

    public void setcFacade(ContactEntityFacade cFacade) {
        this.cFacade = cFacade;
    }

    public AddressEntity getAddress() {
        if (address == null) {
            Collection<AddressEntity> li = selectedVendor.getContact().getAddresses();

            for (AddressEntity a : li) {
                if (a.getType().equals(AddressType.HomeAddress)) {
                    setAddress(a);
                    return a;
                }
            }
        }
        return address;
    }

    public void setAddress(AddressEntity address) {
        address.setType(AddressType.HomeAddress);
        this.address = address;
    }
    private AddressEntity address;
    private AddressEntity addresse;
    private AddressEntity address1 = new AddressEntity();

    public AddressEntity getAddress1() {
        return address1;
    }

    public void setAddress1(AddressEntity address1) {
        address.setType(AddressType.BillingAddress);
        this.address1 = address1;
    }

    public AddressEntity getAddresse1() {
        return addresse1;
    }

    public void setAddresse1(AddressEntity addresse1) {
        this.addresse1 = addresse1;
    }
    private AddressEntity addresse1 = new AddressEntity();

    public AddressEntity getAddresse() {
        if (addresse == null) {
            Collection<AddressEntity> li = selectedVendor.getContact().getAddresses();

            for (AddressEntity a : li) {
                if (a.getType().equals(AddressType.BillingAddress)) {
                    setAddresse(a);
                    return a;
                }
            }
        }
        return addresse;
    }

    public void setAddresse(AddressEntity addresse) {
        this.addresse = addresse;
    }
}
